package com.wyh.lotterysystem.common.utils;

import com.wyh.lotterysystem.dao.dataobject.WinnerRecordDo;
import com.wyh.lotterysystem.service.enums.ActivityPrizeTiersEnum;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.util.List;

@Slf4j
public class SendMailUtil {
    /**
     * 发送邮件验证码
     * @param targetEmail 目标用户邮箱
     * @param authCode 发送的验证码
     */
    public static boolean sendEmailCode(String targetEmail, String authCode) {
        try {
            //创建邮箱对象
            SimpleEmail mail = new SimpleEmail();
            // 设置发送邮件的服务器
            mail.setHostName("smtp.qq.com");
            mail.setSmtpPort(465);
            mail.setSSLOnConnect(true);
            // 发送邮件 "你的邮箱号"+"发送时用的昵称"
            mail.setFrom("2338933935@qq.com", "WYH_LOTTERY");
            // "你的邮箱号"+ "上文开启SMTP获得的授权码"
            mail.setAuthentication("2338933935@qq.com", "hrpmicatclxxdjcf");
            // 接收用户的邮箱
            mail.addTo(targetEmail);
            // 邮件的主题(标题)
            mail.setSubject("注册验证码");
            // 邮件的内容
            mail.setMsg("您的验证码为:" + authCode+"(一分钟内有效)");
            // 发送
            mail.send();
            return true;
        } catch (EmailException e) {
            log.error("验证码发送失败，target({}),{}", targetEmail,e);
            return false;
        }
    }

    public static boolean sendRecord(WinnerRecordDo winnerRecordDo){
        try {
            //创建邮箱对象
            SimpleEmail mail = new SimpleEmail();
            // 设置发送邮件的服务器
            mail.setHostName("smtp.qq.com");
            mail.setSmtpPort(465);
            mail.setSSLOnConnect(true);
            // 发送邮件 "你的邮箱号"+"发送时用的昵称"
            mail.setFrom("2338933935@qq.com", "WYH_LOTTERY");
            // "你的邮箱号"+ "上文开启SMTP获得的授权码"
            mail.setAuthentication("2338933935@qq.com", "hrpmicatclxxdjcf");
            // 接收用户的邮箱
            mail.addTo(winnerRecordDo.getWinnerEmail());
            // 邮件的主题(标题)
            mail.setSubject("中奖通知");
            // 邮件的内容
            ActivityPrizeTiersEnum activityPrizeTiersEnum = ActivityPrizeTiersEnum.forName(winnerRecordDo.getPrizeTier());
            mail.setMsg("恭喜您在【"+ winnerRecordDo.getActivityName() +
                    "】中荣获【" + activityPrizeTiersEnum.getMsg()
                    + "】，奖品为【" + winnerRecordDo.getPrizeName() + "】！");
            // 发送
            mail.send();
            return true;
        } catch (EmailException e) {
            log.error("中奖消息发送失败，target({}),{}", winnerRecordDo.getWinnerEmail(),e);
            return false;
        }
    }
}